//获取应用实例
var app = getApp();
Page({
  data: {
    toastHidden: true, //吐司  
    toastText: '',//吐司文本  
    list: [],
    touch_start: '',
    touch_end: '',
    hidden:true,
  },
  onLoad: function () {
    var self = this;
    getOpenId(self)
  },
  onShow: function () {
    if (app.globalData.OPEN_ID != '') {
      var self = this;
      getData(self);
    }
  },
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
    if (app.globalData.OPEN_ID != '') {
      var self = this;
      getData(self);
    }
  },
  toDetil: function (event) {
    let that = this;
    console.log(event);
    // 取得下标
    var index = parseInt(event.currentTarget.dataset.index);
    // 取出id值
    var AlbumGuid = that.data.list[index].AlbumGuid;
    wx.navigateTo({
      url: "../album/album?AlbumGuid=" + AlbumGuid
    })
  },
  // 删除相册
  deleteAibum(event) {
    let that = this;
    // this.setData({ showActionsSheet: true, imageInAction: event.target.dataset.src });
    console.log(event)
    var position = event.currentTarget.dataset.index;
    console.log(position)
    wx.showModal({
      title: '提示',
      content: '是否删除该相册？',
      success: function (res) {
        if (res.confirm) {
          deleteCategory(that.data.list[position].AlbumGuid, that);
        }
      }
    })
  },
})
//获取OpenId
var getOpenId = function (self) {
  wx.login({
    success: function (res) {
      console.log(res)
      wx.request({
        url: app.globalData.ServiceUrl + 'qiniufile/GetOpenid', //接口地址
        method: 'POST',
        data: {
          ValidateData: app.globalData.ValidateData,
          paras: {
            js_code: res.code
          }
        },
        header: {
          'content-type': 'application/json' // 默认值
        },
        success: function (res) {
          console.log(res.data)
          if (res.data.BusinessInfo.Code == 1 && res.data.ReturnInfo.Code == 1) {
            app.globalData.OPEN_ID = res.data.UserArea.openid;//获取到的openid
            app.globalData.SESSION_KEY = res.data.UserArea.session_key;//获取到session_key
            getData(self);
          } else {
            wx.showToast({
              title: '获取数据失败！',
              image: '../../images/img_jinggao.png',
              duration: 1000,
            })
          }
        }
      })
    }
  })
}

//获取数据
var getData = function (self) {
  wx.request({
    url: app.globalData.ServiceUrl + 'album/getAllAlbumByOwnerUserGuid', //接口地址
    method: 'POST',
    data: {
      ValidateData: app.globalData.ValidateData,
      paras: {
        OwnerUserGuid: app.globalData.OPEN_ID,
        index:1,
        currentPageCount:100
      }
    },
    header: {
      'content-type': 'application/json' // 默认值
    },
    success: function (res) {
      console.log(res.data)
      if (res.data.BusinessInfo.Code == 1 && res.data.ReturnInfo.Code == 1) {
        if (res.data.UserArea.list.length==0){
          self.setData({
            hidden : false,
            list: res.data.UserArea.list
          });
        }else{
          self.setData({
            hidden: true,
            list: res.data.UserArea.list
          });
        }
      } else {
        wx.showToast({
          title: '获取数据失败！',
          image: '../../images/img_jinggao.png',
          duration: 1000,
        })
      }
    }
  })
}
//删除相册
var deleteCategory = function (AlbumGuid, self) {
  console.log(AlbumGuid)
  wx.request({
    url: app.globalData.ServiceUrl + 'album/DeleteAlbum', //接口地址
    method: 'POST',
    data: {
      ValidateData: app.globalData.ValidateData,
      paras: {
        AlbumGuids: AlbumGuid
      }
    },
    header: {
      'content-type': 'application/json' // 默认值
    },
    success: function (res) {
      console.log(res.data)
      if (res.data.BusinessInfo.Code == 1 && res.data.ReturnInfo.Code == 1) {
        wx.showToast({
          title: '删除相册成功！',
          icon: 'success',
          duration: 1000,
          complete: function () {
            //删除成功，重新加载
            getData(self);
          } //接口调用结束的回调函数  
        })

      } else {
        wx.showToast({
          title: '删除相册失败！',
          image: '../../images/img_jinggao.png',
          duration: 1000
        })
      }
    }
  })
}